package com.muyan.singleton.v5;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author menglingchang@kaikeba.com
 * @version : ReentrantLockSinleton5.java, v 0.1 2022-03-20 18:05 menglingchang@kaikeba.com Exp $$
 * @Description 利用可重入锁实现双重检验锁
 */
public class ReentrantLockSinleton5 {

    private static volatile ReentrantLockSinleton5 reentrantLockSinleton5;

    private static ReentrantLock lock = new ReentrantLock();

    private ReentrantLockSinleton5() throws Exception {
        if (reentrantLockSinleton5 != null) {
            throw new Exception("ReentrantLockSinleton5 已被初始化");
        }
        System.out.println("初始化ReentrantLockSinleton5");
    }

    public static ReentrantLockSinleton5 getInstance() {
        if (reentrantLockSinleton5 == null) {
            lock.lock();
            try {
                if (reentrantLockSinleton5 == null) {
                    reentrantLockSinleton5 = new ReentrantLockSinleton5();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
        return reentrantLockSinleton5;
    }
}
